﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;


public class GameUiControl : MonoBehaviour
{
    public static GameUiControl _instance;
    private void Awake()
    {
        _instance = this;
        //DataManager.GetInstance.DataInit();
    }
    public Text rolename;//主角姓名
    public Transform[] Effects;//特效
    public Transform [] skillpos;//技能位置
    public GameObject[] pans;//退出、胜利、失败的panel
    public Transform prent;//创造的boos的父物体
    public Image hp;//hp
    public Transform player;//主角
    public Transform enemypos;//创造敌人的初始位置
    EnemyControl enemy;//敌人boos
    //cd
    public Image[] cds;//技能的cd遮罩
    bool iscd = true;
    bool cd1 = false;
    bool cd2 = false;
    bool cd3 = false;
    float cdvalue1 = 1;
    float cdvalue2 = 1;
    float cdvalue3 = 1;
    public Image[] getRewards;//获得的奖励的图片
    public bool gameover = false;
    private void Start()
    {
        gameover = false;
        rolename.text = VariableDocument.GetInstance.name;
        Const.isgameover = false;
        CreateEnemy();
    }

    private void CreateEnemy()
    {
        //GameObject game = DataManager.GetInstance.Loadasset(ResourcesEnum.Prefab, DataManager.GetInstance.enemdic[VariableDocument.GetInstance.through].enempath);
        GameObject game = DataManager.GetInstance.Loadasset(ResourcesEnum.Prefab, DataManager.GetInstance.enemdic[VariableDocument.GetInstance.through].enempath);
        GameObject temp = Instantiate(game, prent,false);
        enemy = game.GetComponent<EnemyControl>();
        temp.transform.position = enemypos.position;
        enemy.HpValue(DataManager.GetInstance.enemdic[VariableDocument.GetInstance.through].hp);
    }

    private void Update()
    {
        if (cd1)
        {
            cds[0].gameObject.SetActive(true);
            cdvalue1 -= Time.deltaTime;
            cds[0].fillAmount = cdvalue1; // / 1;
            if(cdvalue1<=0)
            {
                cds[0].gameObject.SetActive(false);
                cd1 = false;
                cdvalue1 = 1;
            }
        }
        if (cd2)
        {
            cds[1].gameObject.SetActive(true);
            cdvalue2 -= Time.deltaTime;
            cds[1].fillAmount = cdvalue2/1;
            if (cdvalue2 <= 0)
            {
                cds[1].gameObject.SetActive(false);
                cd2 = false;
                cdvalue2 = 1;
            }
        }
        if (cd3)
        {
            cds[2].gameObject.SetActive(true);
            cdvalue3 -= Time.deltaTime;
            cds[2].fillAmount = cdvalue3; // / 1;
            if (cdvalue3 <= 0)
            {
                cds[2].gameObject.SetActive(false);
                cd3 = false;
                cdvalue3 = 1;
            }
        }

    }
    public void SkillBtn(int id)
    {
        if (!iscd)
            return;
        player.transform.LookAt(GetEnemypos());
        iscd = false;
        skillpos[0].GetComponent<PlayerControl>().PlayerAttack();
        StartCoroutine(Jx());
        switch (id)
        {
            case 0:
                cd1 = true;
                CreateSkill(id);
                break;
            case 1:
                cd2 = true;
                Create2Skill(id);
                break;
            case 2:
                cd3 = true;
                Create3Skill(id);
                break;
            case 4:
                
                break;
        }
        StartCoroutine(AttackCd());
    }
    public void AttackBtn()
    {
        if (!PlayerControl.iscd2)
            return;
        PlayerControl.iscd2 = false;
        player.transform.LookAt(GetEnemypos());
        StartCoroutine(Jx());
        StartCoroutine(AttackCd2());
    }
    IEnumerator Jx()
    {
        yield return new WaitForSeconds(0.4f);
        EnemyJianxue();
    }

    IEnumerator AttackCd()
    {
        
        yield return new WaitForSeconds(0.8f);
        iscd = true;
    }
    IEnumerator AttackCd2()
    {

        yield return new WaitForSeconds(0.8f);
        PlayerControl.iscd2 = true;
    }
    Transform GetEnemypos()
    {
        return GameObject.FindGameObjectWithTag("enemy").transform;
    }


    private void CreateSkill(int id)
    {
        GameObject game = Instantiate(Effects[id].gameObject);
        if(Vector3.Distance(skillpos[0].position, enemy.transform.position) < 5f)
        {
            game.transform.position = enemy.transform.position;
        }
        else
        {
            game.transform.position = skillpos[1].position;
        }
        Const.isbefore = false;

    }
    private void Create2Skill(int id)
    {
        GameObject game = Instantiate(Effects[id].gameObject);
        game.transform.position = skillpos[0].position;
        game.transform.rotation = skillpos[0].transform.rotation;
        Const.isbefore = true;
    }
    private void Create3Skill(int id)
    {
        GameObject game = Instantiate(Effects[id].gameObject);
        game.transform.position = skillpos[0].position;
        Const.isbefore = false;
    }

    float hpvalue = 100;
    public void PlayerJianxue()
    {
        hpvalue -= DataManager.GetInstance.enemdic[VariableDocument.GetInstance.through].demage;
        if (hpvalue <= 0)
        {
            if (Const.isgameover)
                return;
            Const.isgameover = true;
            hp.fillAmount = 0;
            skillpos[0].GetComponent<PlayerControl>().PlayerDie();
            FailureClick();
        }
        else
        {
            hp.fillAmount = hpvalue*0.01f;
            skillpos[0].GetComponent<PlayerControl>().PlayerHit();
        }
    }

    public void EnemyJianxue()
    {
        if (!Const.isjx)
            return;
        enemy.EnemyJx();
    }


    //胜利
    public void VictoryClick()
    {
        gameover = true;
        pans[1].SetActive(true);
        //获得三样装备
        getRewards[0].sprite = GetNewItem._instance.item[VariableDocument.GetInstance.bAndr[VariableDocument.GetInstance.current_reward, 0]].itemImage;
        getRewards[1].sprite = GetNewItem._instance.item[VariableDocument.GetInstance.bAndr[VariableDocument.GetInstance.current_reward, 1]].itemImage;
        getRewards[2].sprite = GetNewItem._instance.item[VariableDocument.GetInstance.bAndr[VariableDocument.GetInstance.current_reward, 2]].itemImage;
        GetNewItem._instance.GetNew(VariableDocument.GetInstance.bAndr[VariableDocument.GetInstance.current_reward, 0]);
        GetNewItem._instance.GetNew(VariableDocument.GetInstance.bAndr[VariableDocument.GetInstance.current_reward, 1]);
        GetNewItem._instance.GetNew(VariableDocument.GetInstance.bAndr[VariableDocument.GetInstance.current_reward, 2]);
    }
    //失败
    public void FailureClick()
    {
        gameover = true;
        pans[2].SetActive(true);
    }
    //点击返回按钮
    public void OpanBackPan()
    {
        gameover = true;
        pans[0].SetActive(true);
    }
    //取消退出
    public void HideBackPan()
    {
        gameover = false;
        pans[0].SetActive(false);
    }
    //返回
    public void YesBtn()
    {
        GoToInterface.ToInterface("9LordReward");
    }
}
